package com.heisenhub.uua.service.login;


import com.heisenhub.uua.service.AbstractCustomTokenGranter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

/**
 *  用户名免密登录
 */
@Slf4j
public class UsernameCodeLoginGranter extends AbstractCustomTokenGranter {

    private UserDetailsService userService;


    private static final String USER_TYPE = "username_code";

    private static final String USERNAME = "username";

    public UsernameCodeLoginGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory , UserDetailsService userService, AuthenticationManager authenticationManager) {
        super(tokenServices, clientDetailsService, requestFactory, USER_TYPE);
        this.userService = userService;

    }

    @Override
    protected UserDetails userLogin(Map<String, String> parameters) {
       // String grantType = parameters.get("grant_type");
        String username = parameters.get(USERNAME);
        return userService.loadUserByUsername(username);
    }
}
